package com.bjpowernode.pmes.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.bjpowernode.pmes.bean.Menu;
import com.bjpowernode.pmes.bean.Page;
import com.bjpowernode.pmes.bean.User;
import com.bjpowernode.pmes.dao.MenuDao;
import com.bjpowernode.pmes.dao.UserDao;
import com.bjpowernode.pmes.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService {

	@Resource
	private UserDao userDao;
	@Resource
	private MenuDao menuDao;

	public User getUserByCode(String usercode) {
		return userDao.getUserByCode(usercode);
	}

	public int insertUser(User user) {
		return userDao.insertUser(user);
	}

	public Page<User> getAllUsers(Integer pageno, Integer pagesize) {
		
		Page<User> page = new Page<User>();
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		
		paramMap.put("startIndex", (pageno-1)*pagesize);
		paramMap.put("pagesize", pagesize);
		
		// 查询集合
		List<User> userList = userDao.getAllUserList(paramMap);
		page.setDataList(userList);
		
		// 查询数量
		Integer count = userDao.getUsersCount();
		page.setTotalsize(count);
		
		if ( count % pagesize == 0 ) {
			page.setTotalno(count / pagesize);
		} else {
			page.setTotalno(count / pagesize+1);
		}
		
		return page;
	}

	public User getUserById(Integer id) {
		return userDao.getUserById(id);
	}

	public int updateUser(User user) {
		return userDao.updateUser(user);
	}

	public int deleteUsers(List<User> userList) {
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("users", userList);
		return userDao.deleteUsers(paramMap);
	}

	public User getUserForLogin(User user) {
		return userDao.getUserForLogin(user);
	}

	public List<Menu> getMenus(Integer pid) {
		return menuDao.getMenus(pid);
	}

	public List<Menu> getAllMenus() {
		return menuDao.getAllMenus();
	}

	public List<Menu> getRoleMenus(String role) {
		return menuDao.getRoleMenus(role);
	}
}
